package com.mbatis.juc;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 异步调用: CompletableFutrue
 * 异步执行
 *  成功回调
 *  失败回调
 */

public class CompletableFutrueDemo {

    // 没有返回值的 runAsync 异步回调
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(()->{
            try {
                Thread.sleep(600);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("OK....");
        });
        System.out.println("1111");

        completableFuture.get();


        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "supplyAsync=> Integer");
            int i = 10/0;
            return 200;
        });

        Integer integer = supplyAsync.whenComplete((t, u) -> {
            System.out.println("t=>" + t);
            System.out.println(
                    "u=>" + u
            );
        }).exceptionally((e) -> {
            System.out.println(e.getMessage());
            return 401;
        }).get();
        System.out.println(integer);


    }
}
